package com.wx.mall.goods.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.goods.dto.StockDto;
import com.wx.mall.goods.entity.Stock;
import com.wx.mall.goods.service.IStockService;
import com.wx.mall.goods.service.impl.StockServiceImpl;
import com.wx.mall.goods.vo.req.StockReqVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author j
 * @since 2020-05-14
 */
@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    StockServiceImpl stockService;

    @ApiOperation(value = "分页查询")
    @GetMapping("/stockPages")
    public CommonResult<CommonPage<Stock>> getStockPagesList(StockDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<Stock> list=stockService.findAll(dto);
        return CommonResult.success(CommonPage.restPage(list));
    }


    @ApiOperation(value = "根据类型为0筛选查询")
    @GetMapping("/stockScreens")
    public CommonResult<CommonPage<Stock>> getStockScreenList(StockDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<Stock> list=stockService.findScreen(dto);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/getDeleteAll")
    public CommonResult<Stock> getDeleteList(StockReqVo vo){
        int deleteList= stockService.deleteList(vo);
        return deleteList==1?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value = "类型为1分页查询")
    @GetMapping("/stockPagesTwo")
    public CommonResult<CommonPage<Stock>> getStockPagesTwoList(StockDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<Stock> list=stockService.findTwo(dto);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "根据类型为0筛选查询")
    @GetMapping("/stockScreenTwo")
    public CommonResult<CommonPage<Stock>> getStockScreenTwoList(StockDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<Stock> list=stockService.findScreenTwo(dto);
        return CommonResult.success(CommonPage.restPage(list));
    }

}

